﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductCategoryCommand
    {
        public static ProductCategoryModel CreateNew(ProductCategoryModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var dCategory = Convertor.ConvertFrom<ProductCategory, ProductCategoryModel>(model);
                    db.DataContext.ProductCategories.Add(dCategory);
                    db.DataContext.SaveChanges();
                    model.ID = dCategory.ID;
                    return model;
                }
            }
            return null;
        }

        public static void Update(ProductCategoryModel model)
        {
            if (model.ID > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCategory = db.DataContext.ProductCategories.FirstOrDefault(p => p.ID == model.ID);
                    if (dCategory == null) throw new ApplicationException("不存在的分类编号");
                    SaveCheck(model, db);
                    Convertor.UpdateValue(model, dCategory);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Delete(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<ProductCategory>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }

        private static void SaveCheck(ProductCategoryModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.CategoryName))
                throw new ApplicationException("请提供分类名称");
            if (db != null)
            {
                if (db.DataContext.ProductCategories.Any(p=>p.CategoryName==model.CategoryName && p.ID!=model.ID))
                    throw new ApplicationException("分类名称重复，请另外输入一个。");
            }
        }

        public static ProductCategoryModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCategory = db.DataContext.ProductCategories.FirstOrDefault(p => p.ID == id);
                    if (dCategory != null)
                    {
                        var model = Convertor.ConvertTo<ProductCategoryModel, ProductCategory>(dCategory);
                        return model;
                    }
                }
            }
            return null;
        }

        public static ProductCategoryCollection GetCategories(ProductCategoryQuery sea)
        {
            var result = new ProductCategoryCollection();
            using (DBCentre db=new DBCentre())
            {
                IQueryable<ProductCategory> q = db.DataContext.ProductCategories;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.CategoryName.Contains(sea.Key));
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<ProductCategoryModel, ProductCategory, ProductCategoryCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }
    }
}
